package com.da.lon.wang.xlg.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.NewPartyLvAdapter;
import com.da.lon.wang.xlg.bean.PartyFragmentBean;
import com.da.lon.wang.xlg.bean.PartyFragmentBean.PartyResult.PartyResultGroupList;
import com.da.lon.wang.xlg.bean.PartyFragmentBean.PartyResult.PartyResultList;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase.Mode;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshListView;
import com.da.lon.wang.xlg.util.ToastUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;

public class NewPartyFragment extends BaseFragment implements
        OnRefreshListener<ListView>, OnClickListener {

    private View view;
    private PullToRefreshListView partyLv;
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (partyLv != null) {
                partyLv.onRefreshComplete();
            }
        }

        ;
    };
    private NewPartyLvAdapter adapter;
    private Context context;
    private ArrayList<PartyResultList> datas;
    private ArrayList<PartyResultGroupList> groupDatas;
    private ImageView frush_img;
    private int is_need_refsh_data = 0;

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("NewPartyFragment");
        MobclickAgent.onPause(getActivity());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        datas = new ArrayList<PartyResultList>();
        groupDatas = new ArrayList<PartyResultGroupList>();
    }

    @Override
    protected View inflateView(LayoutInflater inflater, ViewGroup var2) {
        view = inflater.inflate(R.layout.ocoffee_newparty_fragment, null);
        return view;
    }

    @Override
    protected void initView() {
        registerBaseReceiver();
        context = getActivity();
        initView(view);
        getDatas();
    }

    private void initView(View view) {
        partyLv = (PullToRefreshListView) view
                .findViewById(R.id.ocoffee_newparty_lv);
        frush_img = (ImageView) view.findViewById(R.id.frush_img);
        partyLv.setMode(Mode.PULL_FROM_START);
        partyLv.setOnRefreshListener(this);
        frush_img.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("NewPartyFragment");
        MobclickAgent.onResume(getActivity());
        if (is_need_refsh_data == 1) {
            getDatas();
        }
    }

    private void getDatas() {
        try {
            if (!MyApplication.isNetworkAvailable(getActivity())) {
                showDialog();
                partyLv.setVisibility(View.GONE);
                frush_img.setVisibility(View.VISIBLE);
                is_need_refsh_data = 1;
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                            dismissDialog();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            } else {
                is_need_refsh_data = 0;
                String url = XLGConstants.URL + XLGConstants.URL_PARTY_HEAD
                        + "index";
                HttpLoader<PartyFragmentBean> loader = new HttpLoader<PartyFragmentBean>(
                        context, url, PartyFragmentBean.class);
                AjaxParams params = new AjaxParams();
                params.put(
                        "token",
                        XLGDataUtils.encrypt2MD5("Activity"
                                + XLGConstants.SECRET + "index"));
                loader.loadData(params);
                loader.setManagerListener(new OnWebLoadListener<PartyFragmentBean>() {

                    @Override
                    public void OnStart() {
                        showDialog();

                    }

                    @Override
                    public void OnError(String error) {
                        dismissDialog();
                        handler.sendEmptyMessage(1);
                    }

                    @Override
                    public void onSuccess(PartyFragmentBean t) {
                        dismissDialog();
                        if (t != null && "000000".equals(t.getCode())
                                && t.getResult() != null) {
                            datas = t.getResult().getActivity_list();
                            groupDatas = t.getResult()
                                    .getActivitys_group_list();
                            partyLv.setVisibility(View.VISIBLE);
                            frush_img.setVisibility(View.GONE);
                            handler.sendEmptyMessage(1);
                            if (datas != null) {
                                adapter = new NewPartyLvAdapter(context, datas,
                                        groupDatas);
                                partyLv.setAdapter(adapter);
                            }
                        } else {
                            ToastUtils.showShort(getActivity(), t.getMessage());
                        }
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        try {
            if (!MyApplication.isNetworkAvailable(getActivity())) {
                Toast.makeText(getActivity(), "网络连接失败", Toast.LENGTH_SHORT)
                        .show();
                handler.sendEmptyMessage(1);
            } else {
                partyLv.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        getDatas();
                    }
                }, 1000);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.frush_img:
                getDatas();
                break;

            default:
                break;
        }

    }

    @Override
    protected void RefreshData() {
        // TODO Auto-generated method stub
        getDatas();
    }

}
